package com.pineapple.module.core.hitfertilizer.service;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.pineapple.module.core.hitfertilizer.domain.Hitfertilizer;
import com.pineapple.module.core.hitfertilizer.domain.HitfertilizerCriteria;

public class HitfertilizerManager {
	HitfertilizerDAO dao = null;

	public HitfertilizerManager(){
		dao = new HitfertilizerDAO();
	}

	public List<Hitfertilizer> search(HitfertilizerCriteria criteria,Connection conn)throws Exception{
		List<Hitfertilizer> lstHitfertilizer = new ArrayList<Hitfertilizer>();
		int totalRacord =dao.count(criteria, conn);
		criteria.setTotalResult(totalRacord);

		if (criteria.getTotalResult() < criteria.getStart()) {
			criteria.setStart(criteria.getStart() - criteria.getLinePerPage());
		}
		if(totalRacord>0){
			lstHitfertilizer = dao.search(criteria, conn);
		}
		return lstHitfertilizer;
	}

	public Map<String,String> fertilizerCombo(Connection conn)throws Exception{
		return dao.fertilizerCombo(conn);
	}
	public Map<String,String> areaCombo(Connection conn)throws Exception{
		return dao.searchComboPlantAndArea(conn);
	}
	public void add(Connection conn,Hitfertilizer hitfertilizer)throws Exception{
		dao.add(conn, hitfertilizer);
		if(Integer.parseInt(hitfertilizer.getPlantingStatus())!=2 && Integer.parseInt(hitfertilizer.getPlantingStatus())!=5 ){
			dao.updateStatus(conn, hitfertilizer.getHitfertilizerAreaId() ,Integer.parseInt(hitfertilizer.getPlantingStatus())+1);
		}
	}
	public void edit(Connection conn,Hitfertilizer hitfertilizer)throws Exception{
		dao.edit(conn, hitfertilizer);
	}

	public void delete(Connection conn,String hitfertilizerIds,String plantingId,int status)throws Exception{
		dao.delete(conn, hitfertilizerIds);
		if(dao.countStatus(conn, plantingId)>0){
			dao.updateStatus(conn, plantingId , status-1);
		}
	}
	public Hitfertilizer searchById(Connection conn,int id)throws Exception{
		return dao.searchById(conn, id);
	}

	public void updateStatus(Connection conn,String plantingId,int status) throws Exception{
		dao.updateStatus(conn, plantingId, status);
	}

	public int countNumb(Connection conn,String areaId)throws Exception{
		return dao.countNumb(conn, areaId);
	}
}
